﻿Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports NHibernate
Imports SIAVU.Dominio
Imports NHibernate.Criterion

Namespace Persistencia

    Public Class ProgramacionPersistencia

        Public Overridable Function getHistorialActividades(ByVal ProgramacionID As Long) As IList(Of HistorialActividad)
            Dim session As ISession = Nothing
            Try
                session = NHibernateHelper.OpenSession
                Dim objProgramacion As Programacion
                objProgramacion = DirectCast(session.Get(GetType(Programacion), ProgramacionID), Programacion)
                If objProgramacion.HistorialActividades.Count > 0 Then
                    For Each objHistorialActividad As HistorialActividad In objProgramacion.HistorialActividades
                        If objHistorialActividad.UsuarioModificacion.NombreCompleto Is Nothing Then

                        End If
                    Next
                    Return objProgramacion.HistorialActividades
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw
            Finally
                session.Close()
            End Try
        End Function

        Public Overridable Function getActividades(ByVal ProgramacionID As Long) As IList(Of Actividad)
            Dim session As ISession = Nothing
            Try
                session = NHibernateHelper.OpenSession
                Dim objProgramacion As Programacion
                objProgramacion = DirectCast(session.Get(GetType(Programacion), ProgramacionID), Programacion)
                If objProgramacion IsNot Nothing Then
                    If objProgramacion.Actividades IsNot Nothing AndAlso objProgramacion.Actividades.Count > 0 Then
                        Return objProgramacion.Actividades
                    Else
                        Return Nothing
                    End If
                Else
                    Return Nothing
                End If

            Catch ex As Exception
                Throw
            Finally
                session.Close()
            End Try
        End Function

        'Public Overridable Function getActividades(ByVal ProgramacionID As Long, ByRef Session As ISession) As IList(Of Actividad)
        '    Try
        '        Dim objProgramacion As Programacion
        '        objProgramacion = DirectCast(Session.Get(GetType(Programacion), ProgramacionID), Programacion)
        '        If objProgramacion IsNot Nothing Then
        '            If objProgramacion.Actividades IsNot Nothing AndAlso objProgramacion.Actividades.Count > 0 Then
        '                Return objProgramacion.Actividades
        '            Else
        '                Return Nothing
        '            End If
        '        Else
        '            Return Nothing
        '        End If

        '    Catch ex As Exception
        '        Throw
        '    End Try
        'End Function

        Public Overridable Sub Actualizar(ByVal oProgramacion As Programacion, ByVal oListPostasEliminadas As IList(Of Posta))
            Dim session As ISession = NHibernateHelper.OpenSession()
            Dim oTransaccion As ITransaction = Nothing
            Try
                oTransaccion = session.BeginTransaction()
                oTransaccion.Begin()

                If oListPostasEliminadas IsNot Nothing Then
                    For Each oPosta As Posta In oListPostasEliminadas
                        session.SaveOrUpdate(oPosta) '//Decrementa la cantidad de tripulantes subscriptos
                    Next
                End If

                session.SaveOrUpdate(oProgramacion)
                oTransaccion.Commit()

                session.Close()
            Catch ex As Exception
                oTransaccion.Rollback()
                Throw
            End Try
        End Sub

        ''Public Overridable Function getByFechaModificacionPlazoValida(ByVal FechaValidar As Date, ByVal TipoProgramacion As ETipoProgramacion) As Programacion
        ''    Dim session As ISession = NHibernateHelper.OpenSession
        ''    Try
        ''        Dim criterio As ICriteria = session.CreateCriteria(GetType(Programacion))
        ''        Dim oProgramaciones As IList

        ''        'criterio.Add(Expression.Ge("Plazo.FechaDesdeModificacion", FechaValidar))
        ''        'criterio.Add(Expression.Le("Plazo.FechaHastaModificacion", FechaValidar))
        ''        criterio.Add(Expression.Eq("TipoProgramacion", TipoProgramacion))
        ''        oProgramaciones = criterio.List()

        ''        'session.Close()

        ''        'No deberia de haber mas de 1
        ''        If oProgramaciones.Count > 0 Then
        ''            Return oProgramaciones(0)
        ''        Else
        ''            Return Nothing
        ''        End If

        ''    Catch ex As Exception
        ''        Throw
        ''    End Try
        ''End Function

    End Class

End Namespace

